package homelesspartners.client.model;

import java.io.Serializable;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.RootPanel;

public class User implements Serializable {

	private static final String SPACE = " ";

	private static final long serialVersionUID = 1L;

	public static final int ADMIN = 10;
	public static final int NONE = 0;
	public static final int TYPIST = 1;
	public static final int COORDINATOR = 2;
	public static final int FACILITATOR = 3;
	
	private int userId = -1;
	private int role = -1;
	
	private City currentCity = null;
	private Shelter currentShelter = null;
	private String email = "";
	private String password = null;
	
	private static User instance = new User();
	
	public static User getInstance() {
		return instance;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(userId);
		buffer.append(SPACE);
		buffer.append(role);
		buffer.append(SPACE);
		buffer.append(email);
		buffer.append(SPACE);
		buffer.append(password);
		return buffer.toString();
	}

	public City getCurrentCity() {
		return currentCity;
	}
	
	public void setCurrentCity(City aCity) {
		this.currentCity = aCity;
		String url = "./pages/sponsors/" + aCity.getId() + "_banner.html";
		Element banner = RootPanel.get("banner").getElement();
		DOM.setElementAttribute(DOM.getElementById("banner"), "src", url);
	}
	
	public boolean isValid() {
		if (email.length() < 5 || email.indexOf('@') < 1 || email.lastIndexOf('.') < 3 || email.indexOf('.') > email.length()-2) {
			return false;
		}
		if (password.length() < 4) {
			return false;
		}
		return true;
	}

	public Shelter getCurrentShelter() {
		return currentShelter;
	}

	
	public void setCurrentShelter(Shelter aShelter) {
		this.currentShelter = aShelter;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int aUserId) {
		userId = aUserId;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int aRole) {
		this.role = aRole;
	}

	public boolean isLoggedIn(){
		if(userId > 0){
			return true;
		}
		return false;
	}

	public void updateWith(User user) {
		setUserId(user.getUserId());
		setRole(user.getRole());
	}

	
	public String getEmailAddress() {
		return email;
	}

	
	public void setEmailAddress(String emailAddress) {
		this.email = emailAddress;
	}

	
	public String getPassword() {
		return password;
	}

	
	public void setPassword(String password) {
		this.password = password;
	}
}
